#ifndef YHAIDA_LOGFILE_H
#define YHAIDA_LOGFILE_H
#include "../base/Utils.h"
#include "../mutex/MutexLock.h"
#include <string>
#include <memory>
namespace yhaida
{
    class LogFile : Utils::nocapyable
    {
    public:
        LogFile(const std::string &baseName, size_t rollSize, const int flushInterval, bool isThreadSafe);
        ~LogFile()=default;

        void write(const char *str, int length);
        void flush();

    private:
        void write_unlock(const char *str, int length);
        //根据baseName和当地的时间获取文件名字
        static std::string getLogFileName(const std::string &baseName, time_t *time);
        void rollFile();

    private:
        const std::string _baseName;
        const size_t _rollSize;
        const int _flushInterval;

        int _count; //刷新次数

        time_t _startOfPeriod;
        time_t _lastRoll;
        time_t _lastFlush;

        std::unique_ptr<MutexLock> _mutex;
        std::unique_ptr<Detail::File> _file;

        const static int kCheckTimeRoll = 1024;
        const static int kRollPerSeconds = 60 * 60 * 24; // "second/day"
    };

}

#endif